<template>
	<view>
		<view v-if="isShow">
			<!-- 标题 -->
			<custom-header title="提现账号"></custom-header>

			<!-- 标签切换 -->
			<view class="tabs">
				<view class="tab-item" :class="{active: currentTab === 0}" @click="switchTab(0)">
					支付宝
				</view>
				<view class="tab-item" :class="{active: currentTab === 1}" @click="switchTab(1)">
					银行卡
				</view>
			</view>

			<!-- 空布局内容 -->
			<view class="empty-content" v-if="showEmpty">
				<!-- 图片部分（需要替换实际图片路径） -->
				<image class="empty-image" src="/static/images/ic_nodata.png" mode="widthFix" />

				<!-- 提示文字 -->
				<view class="empty-text">
					<view>暂无{{currentType}}账号，请添加账号</view>
				</view>

				<!-- 底部固定按钮 -->
				<view :class="['footer-button', currentType == '银行卡' && 'disabled']" @click="handleAddAccount">
					{{currentType == '银行卡' ? '暂不支持银行卡' : `添加${currentType}账号`}}
				</view>
			</view>


			<view class="list-container" v-else>
				<!-- 支付宝列表 -->
				<view v-if="currentTab === 0" class="account-list">
					<view class="account-item" v-for="(item, index) in alipayList" :key="index">
						<view class="account-info">
							<image class="account-icon" :src="item.icon"></image>
							<view style="flex: 1; margin-right: 20rpx; display: flex; flex-direction: column;">
								<text class="account-type" style="margin-bottom: 16rpx;">支付宝账号</text>
								<text class="account-number">{{ item.account }}</text>
								<text class="account-type">{{ item.name }}</text>
							</view>
							<view class="detele" @click="toDelete(item.accId)">
								删除账号
							</view>
						</view>
					</view>
				</view>

				<!-- 银行卡列表 -->
				<view v-if="currentTab === 1" class="account-list">
					<view class="account-item" v-for="(item, index) in bankList" :key="index">
						<view class="account-info">
							<text class="bank-name">建设银行</text>
							<text class="card-number">储蓄卡 {{ item.account }}</text>
							<text class="account-name">持卡人：{{ item.name }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		onShow() {
			this.getWithAccount();
		},
		computed: {
			// 判断是否显示空状态
			showEmpty() {
				return this.currentTab === 0 ?
					this.alipayList.length === 0 :
					this.bankList.length === 0
			},
			// 当前账户类型文字
			currentType() {
				return this.currentTab === 0 ? '支付宝' : '银行卡'
			}
		},
		data() {
			return {
				currentTab: 0, // 0-支付宝 1-银行
				accType: 1,
				alipayList: [],
				bankList: [],
				isShow: uni.getStorageSync("isShow"),
			};
		},
		methods: {
			// 获取提现账户
			async getWithAccount() {
				try {
					const res = await this.$http({
						method: 'GET',
						url: '/user/account/list',
						data: {
							accType: this.accType
						}
					});
					console.log(res)

					if (this.currentTab == 0) {
						this.alipayList = res.data
					} else {
						this.bankList = res.data
					}
				} catch (error) {
					//TODO handle the exception
					console.log(error)
				}
			},
			// 提现类型切换
			switchTab(index) {
				this.currentTab = index

				if (this.currentTab == 0) {
					this.accType = 1;
				} else {
					this.accType = 2;
				}

				this.getWithAccount();
			},
			// 添加账号
			handleAddAccount() {
				if (this.currentTab == 0) {
					uni.navigateTo({
						// '/pages/add-bank-account/add-bank-account'添加银行帐号
						url: '/pages/add-alipay-account/add-alipay-account'
					})
				}
			},
			// 删除账号
			toDelete(id) {
				uni.showModal({
					title: '确认删除账号',
					content: '您确定要删除当前账号吗？',
					success: async (res) => {
						if (res.confirm) {
							// 执行删除
							const res = await this.$http({
								method: 'GET',
								url: '/user/account/del',
								data: {
									accId: id
								}
							});
							if (res) {
								this.getWithAccount();
							}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabs {
		display: flex;
		border-bottom: 2rpx solid #eee;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		font-size: 32rpx;
		color: rgba(0, 0, 0, 0.45);
		padding: 20rpx 0;
		transition: all 0.3s;
	}

	.tab-item.active {
		color: #4D7EF4;
		font-weight: bold;
		position: relative;
	}

	.tab-item.active::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 80rpx;
		height: 8rpx;
		border-radius: 4rpx;
		background-color: #4D7EF4;
	}

	.account-list {
		padding: 46rpx;
	}

	.account-item {
		background-color: #EEF7FF;
		padding: 36rpx 40rpx;
		border-radius: 30rpx;
		border-bottom: 1rpx solid #f5f5f5;
	}

	.account-info {
		display: flex;
	}

	.account-icon {
		margin-right: 16rpx;
		height: 48rpx;
		width: 48rpx;
	}

	.account-type,
	.bank-name {
		font-size: 24rpx;
		color: rgba(0, 0, 0, 0.60);
	}

	.account-number,
	.card-number {
		font-size: 24rpx;
		color: #000;
		font-weight: bold;
		margin-bottom: 16rpx;
	}

	.account-name {
		font-size: 24rpx;
		color: #999;
	}

	.detele {
		align-self: center;
		padding: 2rpx 12rpx;
		border: #4D7EF4 solid 2rpx;
		border-radius: 12rpx;
		font-size: 24rpx;
		color: #4D7EF4;
	}

	.empty-content {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -60%);
		text-align: center;
		align-items: center;
		justify-content: center;
		display: flex;
		flex-direction: column;
	}

	.empty-image {
		width: 460rpx;
		height: 460rpx;
		margin-bottom: 38rpx;
	}

	.empty-text {
		font-size: 24rpx;
		color: rgba(0, 0, 0, 0.60);
		margin-bottom: 52rpx;
	}

	.footer-button {
		padding: 18rpx 0;
		text-align: center;
		width: 400rpx;
		background-color: #4D7EF4;
		color: white;
		border-radius: 8rpx;
		font-size: 28rpx;

		&.disabled {
			background-color: #ccc;
		}
	}
</style>